<?php
/**
 * 
 * @version 1.0
 * @since June-2008
 * @package lib
 * @subpackage struct
 * @name CExpression
 * @deprecated Lớp CExpression bao gồm các hàm (các biểu thức)để thao tác với nhiều loại database, hoặc truy vấn...
 *
 */
class CExpression
{
	public $factors;
	public $operator;
	public $memory = false;
	function __construct($operator = NULL, $left_factor = NULL, $right_factor = NULL)
	{
		if($left_factor !== NULL)
		{
			$this->factors[0] = $left_factor;
		}
		if($right_factor !== NULL)
		{
			$this->factors[1] = $right_factor;
		}
		$this->operator = $operator;
	}
	public function __call($name, $arguments) {
		$name = 'E'.$name;
		if(function_exists($name))
		{
			if($this->memory)
			{
				$index = sizeof($this->factors)-1;
				$this->factors[$index] = call_user_func_array($name, $arguments);
				$this->memory = false;
				return $this;
			}
			if(sizeof($arguments)==0 and ($name == 'EAnd' or $name == 'EOr' or $name == 'EEqual' or $name == 'ENE' or $name == 'ELike' or $name == 'ENLike' or $name == 'EBeginWith' or $name == 'EEndWith' or $name == 'EGT' or $name == 'ELT' or $name == 'EGTE' or $name == 'ELTE'))
			{
				$expr = call_user_func_array($name, array($this, EInt(0)));
				$expr->memory = true;
				return $expr;
			}
			return call_user_func_array($name, array_values(array_merge(array($this), $arguments)));
		}
		return $this;
    }
	public function __toString() 
	{	
		if(sizeof($this->factors) == 1)
		{
			return $this->operator.' '.$this->factors[0];
		}
		else 
		{
			$factors = array();
			foreach ($this->factors as $factor) 
			{
				$factors[] = "".$factor;
			}
			return '( '.join(' '.$this->operator.' ', $factors).' )';
		}
	}
	public function parse($func)
	{
		if($this->factors)
		{
			$factors = array();
			foreach ($this->factors as &$factor) 
			{
				if(is_object($factor))
				{
					$factors[] = $factor->parse($func);
				}
				else 
				{					
					if($this->operator == 'INT' or $this->operator == 'REAL'or $this->operator == 'STRING' or $this->operator == 'BOOL' or $this->operator == 'FUNCTION' or is_numeric($factor))
					{
						// Khoanb Edit 07.05.2009
						if($this->operator == 'INT' or $this->operator == 'REAL' or (is_numeric($factor) and $this->operator != 'STRING'))
						{
							$factors[] = floatval($factor);
						}
						else 
						{
							$factors[] = $factor;
						}
					}
					else 
					{
						//eval('$result = '.$func.'(\'STRING\', array($factor));');
						$factors[] = $factor;
					}
				}
			}
			eval('$result = '.$func.'($this->operator, $factors);');
			return $result;
		}
	}
	static function pre_process_conditions($conditions = false)
	{
		if($conditions)
		{
			if(is_array($conditions))
			{
				if(sizeof($conditions) == 1)
				{
					foreach($conditions as $field=>$value)
					{
						$expr = EEqual(EColumn($field), is_int($value)?EInt($value):EString($value));
					}
				}
				else 
				{
					$expr = new CExpression('AND');
					foreach($conditions as $field=>$value)
					{
						$expr->factors[] = EEqual(EColumn($field), is_int($value)?EInt($value):EString($value));
					}
				}
				return $expr;
			}
			elseif(is_object($conditions) and $conditions instanceof CExpression)
			{
				return $conditions;
			}
			elseif(is_string($conditions))
			{
				return EColumn($conditions);
			}
		}
		return false;
	}
}
function EExpr($operator, $args)
{
	$expr = new CExpression($operator);
	foreach ($args as $arg)
	{
		if($arg !== array())
		{
			if($expr->factors)
			{
				if(is_string($arg) or !$arg)
				{
					$expr->factors[] = EString($arg);
					continue;
				}
				else
				if(is_numeric($arg))
				{
					$expr->factors[] = EReal($arg);
					continue;
				}
			}
			if($arg)
			{
				$expr->factors[] = CExpression::pre_process_conditions($arg);
			}
		}
	}
	return $expr;
}
function EAnd()
{
	$args = func_get_args();
	return EExpr('AND', $args);
}
function EOr()
{
	$args = func_get_args();
	return EExpr('OR', $args);
}
function ENot($left_factor)
{
	return new CExpression('!', $left_factor);
}
function EEqual($left_factor, $right_factor)
{
	return EExpr('=', array($left_factor, $right_factor));
}
function ELike($left_factor, $right_factor)
{
	return EExpr('LIKE', array($left_factor, $right_factor));
}
function ENLike($left_factor, $right_factor)
{
	return EExpr('NLIKE', array($left_factor, $right_factor));
}
function EBeginWith($left_factor, $right_factor)
{
	return EExpr('BW', array($left_factor, $right_factor));
}
function EEndWith($left_factor, $right_factor)
{
	return EExpr('EW', array($left_factor, $right_factor));
}
function EMod($left_factor, $right_factor)
{
	return EExpr('%', array($left_factor, $right_factor));
}
function ENE($left_factor, $right_factor)
{
	return EExpr('!=', array($left_factor, $right_factor));
}
function ENNULL($factor)
{
	return new CExpression('NOT_NULL', $factor);
}
function ENULL($factor)
{
	return new CExpression('NULL', $factor);
}
function EGT($left_factor, $right_factor)
{
	return EExpr('>', array($left_factor, $right_factor));
}
function ELT($left_factor, $right_factor)
{
	return EExpr('<', array($left_factor, $right_factor));
}
function EGTE($left_factor, $right_factor)
{
	return EExpr('>=', array($left_factor, $right_factor));
}
function ELTE($left_factor, $right_factor)
{
	return EExpr('<=', array($left_factor, $right_factor));
}

function EFulltextSearch($left_factor, $right_factor)
{
	return EExpr('FULLTEXT', array($left_factor, $right_factor));
}

function ENIn($left_factor, $right_factor){
	return EExpr('NOT IN', array($left_factor, $right_factor));
}

function EIn($field)
{
	$expr = new CExpression('IN');
	$expr->factors[] = $field;
	$args = func_get_args();
	if(sizeof($args)>1 and is_array($args[1]))
	{
		foreach ($args[1] as $arg)
		{
			$expr->factors[] = is_int($arg)?EInt($arg):EString($arg);
		}
	}
	else 
	{
		for ($i = 1; $i<sizeof($args); $i++)
		{
			$expr->factors[] = is_int($args[$i])?EInt($args[$i]):EString($args[$i]);
		}
	}
	return $expr;
}
function EColumn($left_factor)
{
	return new CExpression('COLUMN', $left_factor);
}
function EId($left_factor)
{
	return new CExpression('ID', EString($left_factor));
}
function EString($name)
{
	return new CExpression('STRING', $name);
}
function EInt($name)
{
	return new CExpression('INT', $name);
}
function EReal($name)
{
	return new CExpression('REAL', $name);
}
function EBool($name)
{
	return new CExpression('BOOL', $name);
}
function EFunction()
{
	$expr = new CExpression('FUNCTION');
	$args = func_get_args();
	foreach ($args as $arg)
	{
		$expr->factors[] = $arg;
	}
	return $expr;
}
function E($st)
{
	return new CExpression('EXPRESSION', $st);
}
?>